import {Component, ViewChild} from '@angular/core';
import {AlertController, Content, MenuController, NavController, NavParams, Platform} from 'ionic-angular';
import {Dish} from "../../../models/Dish";
import {DishProvider} from "../../../providers/DishProvider";
import {CustomerPage} from "../../../core/foundations/CustomerPage";
import {CustomerProvider} from "../../../providers/customerProvider";
import {CategoryProvider} from "../../../providers/category";
import {Category} from "../../../models/Category";
import {KitProvider} from "../../../core/foundations/KitProvider";
import {CartProvider} from "../../../providers/cartProvider";
import {Observable} from "rxjs/Observable";
import {MenuPage} from "../menu/menu";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage extends CustomerPage {
  @ViewChild(Content) content: Content;
  dishes: Dish[];
  total = 0;
  category: number;
  keywords: string;
  categories: Category[];
  nextPage = 1;
  noMore = false;
  carts;


  constructor(public nav: NavController,
              public navParams: NavParams,
              public menu: MenuController,
              public alert: AlertController,
              public auth: CustomerProvider,
              public dishProd: DishProvider,
              public cateProd: CategoryProvider,
              public cartProd: CartProvider,
              public kit: KitProvider, private platform: Platform) {
    super();
    this.checkLogin();

  }


  ionViewDidLoad() {
    let loading = this.kit.loadingPresent();
    this.getCategories();
    this.cartProd.getCart().catch(err => {

        this.category = this.navParams.get('category');
        console.log('nav category',this.category);
        loading.dismiss();
        console.log('Error:', err);
        return Observable.throw(err);
      }
    )
      .subscribe(res => {
        loading.dismiss();

        this.carts = this.cartProd.toMap();
        console.log('Home Carts:', this.carts);
      });


  }

  private getCategories() {
    this.cateProd.getCategories().subscribe(categories => {
      this.categories = categories;
      this.getDishes();
    });
  }

  private getDishes() {
    this.keywords = localStorage.getItem('keywords');
    console.log("this.keywords",this.keywords);
    if (this.navParams.get('keywords') != null)
      this.keywords = this.navParams.get('keywords');
    let loading = this.kit.loadingPresent();
    this.dishProd.getDishes(1, this.category, this.keywords).subscribe(res => {
      loading.dismiss();
      console.log(this.nextPage);

      if (this.nextPage == res.last_page || res.last_page == 0)
        this.noMore = true;
      else
        this.nextPage = this.nextPage + 1;
      console.log(this.nextPage);

      this.dishes = res.data;
      console.log('this.dishes', this.dishes);
      this.total = res.total;
      this.content.resize();
    });
  }

  getMore(event) {
    if (!this.noMore) {
      let loading = this.kit.loadingPresent();
      this.dishProd.getDishes(this.nextPage, this.category, this.keywords).subscribe(res => {
        console.log(res);
        loading.dismiss();
        if (this.nextPage == res.last_page)
          this.noMore = true;

        else
          this.nextPage = this.nextPage + 1;
        console.log(this.nextPage);
        for (let d of res.data) {
          this.dishes.push(d as Dish);
        }
        event.complete();
      });
    } else
      event.complete();
  }

  getDishesbyCategory(id: number) {
    this.category = id;
    console.log('this.category', this.category);
    this.nextPage = 1;
    this.getDishes();
  }

  onNumChanged(event, dish) {
    this.kit.cart.changeCart(dish, event.num).subscribe();
  }

  numberOfCarts(dish_id: number): number {
    let cart = this.carts.get(dish_id.toString());

    if (cart != null) {
      return cart.Number_of_Orders;
    }
    else return 0;
  }

  showAll() {
    this.nav.push(MenuPage);
  }



}
